package com.ssm.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssm.entity.Dict;
import com.ssm.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import com.ssm.dao.DictDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author jie_ge
 * @Date 2022/4/18
 */
@Service(interfaceClass = DictService.class)
public class DictServiceImpl implements DictService {
    @Autowired
    private DictDao dictDao;

    /**
     * 根据id获取节点数据列表
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> findZNodes(Long id) {
        // 返回数据[{ id:2, isParent:true, name:"随意勾选 2"}]
        //根据id获取子节点数据
        List<Dict> dictList = dictDao.findListByParentId(id);
        //构建ztree数据
        List<Map<String,Object>> zNodes = new ArrayList<>();
        for (Dict dict: dictList) {
            //判断该节点是否是父节点
            Integer count = dictDao.countIsParent(dict.getId());
            Map<String,Object> map = new HashMap<>();
            map.put("id",dict.getId());
            map.put("isParent",count > 0 ? true:false);
            map.put("name",dict.getName());
            //全部权限列表
            zNodes.add(map);
        }
        return zNodes;
    }

    /**
     * 根据上级id获取子节点数据列表
     * @param parentId
     * @return
     */
    @Override
    public List<Dict> findListByParentId(Long parentId) {
        return dictDao.findListByParentId(parentId);
    }

    /**
     * 根据编码获取子节点数据列表
     * @param dictCode
     * @return
     */
    @Override
    public List<Dict> findListByDictCode(String dictCode) {
        Dict dict = dictDao.getByDictCode(dictCode);
        if(null == dict){
            return null;
        }
        return findListByParentId(dict.getId());
    }

    /**
     * 根据id获取名字
     * @param id
     * @return
     */
    @Override
    public String getNameById(Long id) {
        return dictDao.getNameById(id);
    }
}
